<?php
namespace Home\Controller;
use Think\Controller;
class PfcController extends Controller {
    public function index(){
        indexRvent();//测算首页执行事件
        cookie('orderidnum','Pfc');
        $this->assign('titleName',titleName('Pfc',1));//首页名称

        indexRequest($_REQUEST);//测算首页接收数据处理

        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);

            if($_SERVER['HTTP_HOST']=='hy.yixueqm.com'){
                if($_REQUEST['openid']!=''){
                    cookie('openid',$_REQUEST['openid'],7100);
                    S('ticket',$_REQUEST['ticket'],7100);
                }
                $openid=cookie('openid');
                $wzurl=wxJsdkData();
                if(empty($openid)){
                    openidJump('Pfc',cookie('channel'));
                }
            }else{
                getToken();
                getTicket();
                $noncestr=noncestr(15);
                $timestamp=time();
                $ticket= S('ticket');
                $wzurl=wxJsdkData();
                $signature=sha1("jsapi_ticket={$ticket}&noncestr={$noncestr}&timestamp={$timestamp}&url={$wzurl}");

                $this->assign('appid',cookie('APPID'));
                $this->assign('timestamp',$timestamp);
                $this->assign('noncestr',$noncestr);
                $this->assign('signature',$signature);
//            wxShareUrl('CY','Bzcy');//分享地址获取

                if(!empty($_REQUEST['code'])){
                    cookie('code',$_REQUEST['code'],7100);
                }
                getCode($wzurl);
                getOopenid();
            }
            cookie('uid',cookie('openid'));
        }


        if(cookie('jploginHC')==''){
            cnzz_pvuv('PF');//执行统计
            cookie('jploginHC',1,30);
        }

        $this->assign('orderidnum','Pfc');
        $this->display("Pfc/ZM_pfc");
    }
    public function pfc_zhifu(){
        $this->assign('titleName',titleName('Pfc',2));//首页名称
        $user_agent = $_SERVER['HTTP_USER_AGENT'];//用户使用的浏览器，操作系统等信息。
        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
            cookie('wxlogin',0);
        }else{
            cookie('wxlogin',1);
            $this->assign('wxlogin',cookie('wxlogin'));
        }

        if($_REQUEST['birthday']){//返回操作
            cookie('znickname','匿名');
            cookie('zymd',$_REQUEST['birthday']);
            cookie('eymd',$_REQUEST['birthdayEnd']);
            cookie('zsex',$_REQUEST['gender']);

            $mealopt=$_REQUEST['mealopt'];
            if($mealopt==0)cookie('price',998);//价格
            if($mealopt==1)cookie('price',1888);//价格
            if($mealopt==2)cookie('price',1888);//价格
        }

        if(!empty($_REQUEST['ordernum'])){//未支付订单
            $arrdata[0]=S('sm_h5test|'.$_REQUEST['ordernum']);
            $zymd=$arrdata[0]['year'].'-'.str_pad($arrdata[0]['month'],2,"0",STR_PAD_LEFT).'-'.str_pad($arrdata[0]['day'],2,'0',STR_PAD_LEFT);
            cookie('zymd',$zymd);
            cookie('zhour',$arrdata[0]['hour']);
            cookie('znickname',$arrdata[0]['username']);
            cookie('zsex',$arrdata[0]['sex']);
            cookie('datetype',$arrdata[0]['datetype']);
            cookie('orderid',$_REQUEST['ordernum']);
            cookie('price',$arrdata[0]['price']);
        }

        $data=cookie('orderid');//获取订单号
        if(empty($data)&&empty($_REQUEST['ordernum'])){
            $orderid='PF'.date('Ymd').mt_rand(100,999).date('His');
            cookie('orderid',$orderid);
        }

        //创建订单
        $hour=cookie('zhour');
        $shop=cookie('orderid');
        $znickname=cookie('znickname');if($znickname=='')$znickname='匿名';
        $zsex=cookie('zsex');if($zsex=='')$zsex=1;
        $channel=cookie('channel');
        $imei=cookie('uid');if(cookie('uid')==''){$imei=cookie('imei');}
        $y=mb_substr(cookie('zymd'),0,4);//年
        $m=mb_substr(cookie('zymd'),5,2); //月
        $d=mb_substr(cookie('zymd'),8,2);//日
        $eymd=cookie('eymd');


//        $channelPayArr=M()->query("select pay from tb_channel_pay where channel='{$channel}' limit 1");//查询渠道价格
//        if($channelPayArr){cookie('price',$channelPayArr[0]['pay']);}

        if(cookie('couponPrice')){//是否有使用优惠券
            cookie('price',cookie('couponPrice'));
            $this->assign('discount',10);//优惠券价格
        }else{
            $this->assign('discount',0);//优惠券价格
        }

        $datastr=stristr(cookie('znickname'),'测试');
        if($datastr||$channel=='qudao100'){cookie('price',0.01,604800);}

        $price=cookie('price');
        if(empty($_REQUEST['ordernum'])){
//            $arrdata=M()->query("select id from sm_h5test where ordernum='{$shop}' limit 1");
//            if(empty($arrdata)){
//                M()->query("insert into sm_h5test (ordernum,price,username,typeid,sex,status,year,month,day,paykind,remark,appuserid,ip,channel)values(
//                                              '{$shop}','{$price}','{$znickname}',30,{$zsex},-1,{$y},{$m},{$d},-1,'{$eymd}','{$imei}','{$_SERVER['REMOTE_ADDR']}','{$channel}')");
//                $IndexController=A('Index');//Index控制器
//                $IndexController->subNumber('PF');//提交订单次数统计
//            }
//            if(!cookie($_REQUEST['birthday'])){//返回操作
//                //更新订单
//                M()->query("update sm_h5test set username='{$znickname}',year={$y},month={$m},day={$d},sex={$zsex},remark='{$eymd}',price='{$price}' where ordernum='{$shop}'");
//            }
            $arrdata=S('sm_h5test|'.$shop);
            if(empty($arrdata)){
                $smH5testinter=array(
                    'ordernum'=>$shop, 'price'=>$price, 'username'=>$znickname, 'typeid'=>30, 'sex'=>$zsex, 'createtime'=>date('Y-m-d H:i:m'),
                    'status'=>-1, 'year'=>$y, 'month'=>$m, 'day'=>$d, 'hour'=>$hour,
                    'appuserid'=>$imei, 'ip'=>$_SERVER['REMOTE_ADDR'], 'channel'=>$channel, 'auto_send_sms'=>0, 'place'=>0,
                );
                S('sm_h5test|'.$shop,$smH5testinter,2592000);//2592000为30天
                cacheZhiFuOrderid($shop);//支付页缓存订单操作

                $IndexController=A('Index');//Index控制器
                $IndexController->subNumber('PF');//提交订单次数统计
            }else if(!cookie($_REQUEST['username'])){//返回操作
                //更新订单
                $arrdata['username']=$znickname;
                $arrdata['year']=$y;$arrdata['month']=$m;$arrdata['day']=$d;$arrdata['sex']=$zsex;
                S('sm_h5test|'.$shop,$arrdata,2592000);//2592000为30天
            }
        }

        if (strpos($user_agent, 'MicroMessenger') == false) {
            //非微信浏览器访问
        }else{
            $xml=wxgzhzf('PF','剖腹产',cookie('orderid'));//公众号支付回调参数
            $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
            $val = json_decode(json_encode($xmlstring),true);
            $noncestr=noncestr(15);//随机字符串
            $timestamp=time();//时间戳

            $stringA="appId=".cookie('APPID')."&nonceStr={$noncestr}&package=prepay_id={$val['prepay_id']}&signType=MD5&timeStamp={$timestamp}";
            $stringSignTemp=$stringA."&key=".cookie('wxKEY');
            $sign=strtoupper(md5($stringSignTemp));

            $this->assign('timeStamp',$timestamp);
            $this->assign('nonceStr',$noncestr);
            $this->assign('package','prepay_id='.$val['prepay_id']);
            $this->assign('signType','MD5');
            $this->assign('paySign',$sign);
            $this->assign('appId',cookie('APPID'));
        }
        $this->assign('csNameJump',U('Pfc/jieguoye','',false).'?ordernum='.cookie('orderid'));//支付成功跳转地址

        $this->assign('channel',cookie('channel'));
        $this->display("Pfc/ZM_pfc_zhifu");
    }
    public function jieguoye(){
        $this->assign('titleName',titleName('Pfc',3));//首页名称
        $this->display("Pfc/ZM_pfc_zhifu_jieguoye");
    }

    public function pay(){
        $this->assign('ordername1','剖腹产');
        $this->assign('ordername2','知命-剖腹产');
        $this->display("Index/pay");
    }
    public function paywx(){
        H5ZMwxzhifu('PF','剖腹产',cookie('orderid'),'Pfc');//知命
    }
}